home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / lang / c / pthd-0.000 / pthd-0 / pthd-0.7 / src_signals / sigwait.c.1 < prev    next >
Encoding:
Text File  |  1995-08-16  |  1.9 KB  |  93 lines

  1. /*
  2.  * sigwait.c.1   Tests the sigwait() service.  Create 4 sigwaiters each of
  3.  *               which will wait for the SIGINT, SIGTERM, SIGHUP, and SIGQUIT 
  4.  *               signals.
  5.  */
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <pthread.h>
  9. #include "utils.h"
  10.  
  11. #define THREADS ((int) 4)
  12. extern int getpid( void );
  13.  
  14. void
  15. waiter( void )
  16. {
  17.     sigset_t sigset;
  18.     int caught = FAILURE;
  19.  
  20.    sigemptyset( &sigset );
  21.    sigaddset( &sigset, SIGINT );
  22.    sigaddset( &sigset, SIGTERM );
  23.    sigaddset( &sigset, SIGHUP );
  24.    sigaddset( &sigset, SIGQUIT );
  25.  
  26.    pthread_sigmask( SIG_SETMASK, &sigset, NULL );
  27.  
  28.     switch((caught = sigwait( sigset )))
  29.     {
  30.       case SIGINT:
  31.         print_str("Caught SIGINT");
  32.         break;
  33.       case SIGTERM:
  34.         print_str("Caught SIGTERM");
  35.         break;
  36.       case SIGHUP:
  37.         print_str("Caught SIGHUP");
  38.         break;
  39.       case SIGQUIT:
  40.         print_str("Caught SIGQUIT");
  41.         break;
  42.       default:
  43.                 printf("Error: %d\n", caught );
  44.         break;
  45.  
  46.     }
  47.  
  48.     pthread_exit( (void *) caught );
  49. }
  50.  
  51. static pthread_t th[THREADS];
  52.  
  53. int 
  54. main( int argc, char *argv[] )
  55. {
  56.    int exit_status, st, i;
  57.    pthread_attr_t attr;
  58.  
  59.    printf("pid %d: Blocked %d %d %d %d\n", 
  60.            getpid(), SIGINT, SIGQUIT, SIGHUP, SIGTERM );
  61.  
  62.    /*
  63.     * Create some joinable threads each of which will inherit the
  64.     * main threads mask of blocked signals.
  65.     */
  66.    st = pthread_attr_init( &attr );
  67.    CHECK( st, "pthread_attr_init()");
  68.  
  69.    st = pthread_attr_setdetachstate( &attr, PTHREAD_CREATE_JOINABLE );
  70.    CHECK( st, "pthread_attr_setdetachstate()");
  71.    for(i = 0; i < THREADS; i++ )
  72.    {
  73.        st = pthread_create( &th[i], &attr, (thread_proc_t) waiter, NULL ); 
  74.        CHECK(st, "pthread_create()");
  75.    }
  76.  
  77.    for(i = 0; i < THREADS; i++ )
  78.    {
  79.        st = pthread_join( th[i], (void **) &exit_status );
  80.        CHECK( st, "pthread_join()");
  81.    }
  82.  
  83.    st = pthread_attr_destroy( &attr );
  84.    CHECK( st, "pthread_attr_destroy()");
  85.  
  86.    return( EXIT_SUCCESS );
  87. }
  88.  
  89.  
  90.  
  91.  
  92.  
  93.